package cn.buyclss.backend.service.impl;

import cn.buyclss.backend.entity.domain.User;
import cn.buyclss.backend.entity.domain.UserExample;
import cn.buyclss.backend.entity.domain.UserRole;
import cn.buyclss.backend.entity.domain.UserRoleRelation;
import cn.buyclss.backend.entity.support.RespResult;
import cn.buyclss.backend.entity.vo.RegisterUserVO;
import cn.buyclss.backend.mapper.UserMapper;
import cn.buyclss.backend.mapper.UserRoleMapper;
import cn.buyclss.backend.service.UserService;
import cn.buyclss.backend.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Cecurio
 * @create: 2019-02-19 11:40
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<User> getAll() {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(1);
        return userMapper.selectByExample(example);
    }

    @Override
    public User getUserByUsernameAndPassword(String username, String password) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(username);
        criteria.andDeletedEqualTo(0);
        criteria.andPassWordEqualTo(MD5Util.MD5Encode(password));
        List<User> users = userMapper.selectByExample(example);
        if (users == null || users.size() < 1) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public User getUserByUsername(String username) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(username);
        List<User> users = userMapper.selectByExample(example);
        if (users == null || users.size() < 1) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public UserRoleRelation getRolesByUserId(Integer id) {
        UserRoleRelation userRole = userMapper.getRolesByUserId(id);
        return userRole;
    }

    @Override
    public RespResult addOne(RegisterUserVO userVO) {
        RespResult res = RespResult.successInstance();
        // 校验验证码
        if (!"b2bc".equals(userVO.getEmailVerificationCode())) {
            return RespResult.failureInstance("验证码不正确");
        }

        // 校验两次密码
        if (!userVO.getPassWord().equals(userVO.getConfirmPassword())) {
            return RespResult.failureInstance("两次密码不一致");
        }

        // 查重
        UserExample example = new UserExample();
        UserExample.Criteria cr = example.createCriteria();
        cr.andUserNameEqualTo(userVO.getUserName());
        List<User> userList = userMapper.selectByExample(example);
        if (userList != null && userList.size() > 0) {
            return RespResult.failureInstance("此用户名已被占用哦^_^，请换一个!");
        }

        // 插入到数据库
        User user = new User();
        user.setUserName(userVO.getUserName());
        user.setPassWord(MD5Util.MD5Encode(userVO.getPassWord()));
        user.setLogoUrl("http://resource.cecurio.cn/buy-clss-default-head-photo.jpg");
        user.setType(1);
        user.setHasShop(0);
        user.setDeleted(0);
        user.setGmtCreate(new Date());
        user.setGmtModified(new Date());

        int insertId = userMapper.insertSelective(user);


        // 给用户分配角色
        Integer newUserId = user.getId();
        UserRole userRole = new UserRole();
        userRole.setUserId(newUserId);
        userRole.setIsDeleted(false);
        userRole.setRoleId(2);
        userRole.setGmtCreate(new Date());
        userRole.setGmtModified(new Date());
        userRoleMapper.insertSelective(userRole);

        return res;
    }
}
